package com.fastloan.jn.mgt.customer.service.impl;

import com.fastloan.jn.common.BasicServiceImpl;
import com.fastloan.jn.mgt.customer.dao.JnCustomerTransitionMapper;
import com.fastloan.jn.mgt.customer.enums.JnCustomerTransitionEnum;
import com.fastloan.jn.mgt.customer.model.JnCustomer;
import com.fastloan.jn.mgt.customer.model.JnCustomerTransition;
import com.fastloan.jn.mgt.customer.model.vo.JnCustomerTransitionVo;
import com.fastloan.jn.mgt.customer.service.JnCustomerService;
import com.fastloan.jn.mgt.customer.service.JnCustomerTransitionService;
import com.fastloan.safe.core.util.ManagerKit;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import com.kawins.supers.table.manager.DataTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author chenkai
 *         createTime 2017-10-2017/10/18 9:40
 */
@Service
public class JnCustomerTransitionServiceImpl extends BasicServiceImpl<JnCustomerTransition> implements JnCustomerTransitionService {
    @Autowired
    private JnCustomerTransitionMapper customerTransitionMapper;

    @Autowired
    private JnCustomerService customerService;

    @Override
    @DataTable(name = "customerTransitionApply")
    public List<JnCustomerTransitionVo> customerTransitionApply(Map<String, Object> map) {
        map.put("managerId", ManagerKit.getUserId());
        return customerTransitionMapper.customerTransitionApply(map);
    }

    @Override
    @DataTable(name = "customerTransitionApprove")
    public List<JnCustomerTransitionVo> customerTransitionApprove(Map<String, Object> map) {
        return customerTransitionMapper.customerTransitionApprove(map);
    }

    @Override
    public ResultTo apply(JnCustomerTransition customerTransition) {
        JnCustomer customer = customerService.selectByPrimaryKey(customerTransition.getCustomerId());
        customerTransition.setOldCustomerManagerId(customer.getManagerId());
        return insertSelective(customerTransition) != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultTo pass(String customerId) {
        JnCustomerTransition jnCustomerTransition = selectByPrimaryKey(customerId);
        jnCustomerTransition.setTransitionStatus(JnCustomerTransitionEnum.APPROVAL.getType());
        jnCustomerTransition.setOperationTime(new Date());
        updateByPrimaryKeySelective(jnCustomerTransition);

        JnCustomer jnCustomer = customerService.selectByPrimaryKey(customerId);
        jnCustomer.setManagerId(jnCustomerTransition.getNewCustomerManagerId());
        return customerService.updateByPrimaryKeySelective(jnCustomer) != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    @Override
    public ResultTo reject(String customerId) {
        JnCustomerTransition jnCustomerTransition = selectByPrimaryKey(customerId);
        jnCustomerTransition.setTransitionStatus(JnCustomerTransitionEnum.APPROVAL_REJECT.getType());
        jnCustomerTransition.setOperationTime(new Date());
        return updateByPrimaryKeySelective(jnCustomerTransition) != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }
}
